//
//  ZYCurrencyExchangeController.m
//  BestLearning
//
//  Created by Ethan on 2017/9/13.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYCurrencyExchangeController.h"

@interface ZYCurrencyExchangeController ()
@property (nonatomic,strong) UIImageView *photoImageView;
@property (nonatomic,strong) UILabel *redRmbLabel;
@property (nonatomic,strong) UIButton *currencyButton;//兑换比例
@property (nonatomic,strong) UILabel *starExchangeLabel;//兑换星币
@property (nonatomic,strong) UITextField *starinputTextField;
@property (nonatomic,strong) UILabel *balanceOfCurrencyLabel;//星币余额
@property (nonatomic,strong) UIButton *sureButton;
@property (nonatomic,copy)NSString *flowers;

@end

@implementation ZYCurrencyExchangeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"星币兑换";
    self.view.backgroundColor = ColorRGB(247, 244, 248);

    
//
//    UIBarButtonItem *liftItem = [UIBarButtonItem itemWithTargat:self action:@selector(lifetimeAction) image:@"navigationbar_back" highImage:@"navigationbar_back"];
//    self.navigationItem.leftBarButtonItem = liftItem;
    
    
    [self setUpSuperView];
    [self setNetWork];
}


#pragma mark --- 返回方法的实现
- (void)lifetimeAction{
    
    if (self.backPop) {
        self.backPop();
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)setNetWork{
    
    NSString *url = [NSString stringWithFormat:@"user.do?action=getMyInfoAndRecharge"];
    
    NSLog(@"%@  %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
  
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            self.balanceOfCurrencyLabel.text = [NSString stringWithFormat:@"星币余额：%ld",[[t valueForKey:@"money"]  integerValue]];
            NSMutableAttributedString *hintStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"红花余额 %ld朵",[[t valueForKey:@"flowers"] integerValue]]];
            //获取要调整颜色的文字位置,调整颜色
            NSRange range=[[hintStr string]rangeOfString: [NSString stringWithFormat:@"%ld朵",[[t valueForKey:@"flowers"] integerValue]]];
            [hintStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
            [hintStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:23] range:range];
            self.redRmbLabel.attributedText=hintStr;
            self.flowers = [NSString stringWithFormat:@"%ld",[[t valueForKey:@"flowers"] integerValue]];
        }
        
        
        
    } Fail:^(NSError *Error) {
          [SVProgressHUD dismiss];
    }];
    
    
    
}




- (void)setUpSuperView{
    
    self.photoImageView = [UIImageView new];
    self.photoImageView.image = [UIImage imageNamed:@"bg_balance_default"];
    self.photoImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:self.photoImageView];
    
    self.redRmbLabel = [UILabel new];
    self.redRmbLabel.font = [UIFont systemFontOfSize:12];
    self.redRmbLabel.textAlignment = NSTextAlignmentCenter;
    [self.photoImageView addSubview:_redRmbLabel];
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.width.offset(kScreenWidth);
        make.height.offset(180);
    }];
    
    [self.redRmbLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.width.offset(kScreenWidth);
        make.bottom.equalTo(self.photoImageView).offset(-20);
    }];
    
    
    self.currencyButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    [self.currencyButton setBackgroundImage:[UIImage imageNamed:@""] forState:(UIControlStateNormal)];
    [self.currencyButton setTitle:@"兑换比例：500朵红花=1个星币" forState:(UIControlStateNormal)];
    self.currencyButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.currencyButton setTitleColor:ColorRGB(153, 153, 153) forState:(UIControlStateNormal)];
    [self.view addSubview:self.currencyButton];
    
    [self.currencyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.photoImageView);
        make.top.equalTo(self.photoImageView.mas_bottom).offset(0);
        make.width.offset(kScreenWidth);
        make.height.offset(44);
    }];
    
    
    UIView *allView = [UIView new];
    allView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:allView];
    
    [allView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.currencyButton);
        make.top.equalTo(self.currencyButton.mas_bottom).offset(0);
        make.width.offset(kScreenWidth);
        make.height.offset(44);
    }];
    
    self.starExchangeLabel = [UILabel new];
    self.starExchangeLabel.font = [UIFont systemFontOfSize:15];
    self.starExchangeLabel.text = @"兑换星币";
    self.starExchangeLabel.textColor = ColorRGB(51, 51, 51);
    [allView addSubview:self.starExchangeLabel];
    
    [self.starExchangeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(allView).offset(15);
        make.top.equalTo(allView).offset(0);
        make.width.offset(80);
        make.height.offset(44);
    }];
    
    self.starinputTextField = [UITextField new];
    self.starinputTextField.placeholder = @"输入需要兑换的星币数";
    self.starinputTextField.textColor = ColorRGB(153, 153, 153);
    self.starinputTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.starinputTextField.textAlignment = NSTextAlignmentCenter;
    self.starinputTextField.font = [UIFont systemFontOfSize:15];
    [allView addSubview:self.starinputTextField];
    
    [self.starinputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.starExchangeLabel.mas_right).offset(5);
        make.top.centerY.equalTo(self.starExchangeLabel);
        make.width.offset(kScreenWidth - 30 - 80);
    }];
    
    
    self.balanceOfCurrencyLabel = [UILabel new];
    self.balanceOfCurrencyLabel.textAlignment = NSTextAlignmentRight;
    self.balanceOfCurrencyLabel.font = [UIFont systemFontOfSize:13];
    self.balanceOfCurrencyLabel.textColor = ColorRGB(153, 153, 153);
    [self.view addSubview:self.balanceOfCurrencyLabel];
    

    [self.balanceOfCurrencyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(allView.mas_bottom).offset(20);
    }];
    
    
    
    
    self.sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.sureButton setTitle:@"兑换" forState:(UIControlStateNormal)];
    [self.sureButton setTitleColor:ColorRGB(255, 255, 255) forState:(UIControlStateNormal)];
    self.sureButton.titleLabel.font = [UIFont systemFontOfSize:23];
    self.sureButton.layer.cornerRadius = 10;
    self.sureButton.clipsToBounds = YES;
    [self.sureButton setBackgroundImage:[UIImage imageNamed:@"btn_ensure_defaultt"] forState:(UIControlStateNormal)];
    [self.view addSubview:self.sureButton];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.equalTo(allView.mas_bottom).offset(80);
        make.left.offset(20);
        make.width.offset(kScreenWidth - 40);
        make.height.offset(40);
        
    }];
    
    [self.sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    
}



#pragma mark ---- 兑换方法的实现
- (void)sureButtonAction{
   
    if (self.starinputTextField.text.length == 0) {
        [self createAlertViewWithTitle:nil message:@"请输入兑换的星币数" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
    }else if ([self.starinputTextField.text integerValue] == 0){
        [self createAlertViewWithTitle:nil message:@"请输入大于0的星币数" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
    }else{
        
        if ([self.starinputTextField.text integerValue] *500 <= [self.flowers integerValue]) {
            NSString *uid = [AccountTool shareAccountTool].currentAccount.uid;
            NSString *url = [NSString stringWithFormat:@"user.do?action=flowerReplaceCoins&replaceCoins=%@&uid=%@",self.starinputTextField.text,uid];
            
            
            NSLog(@"%@  %@",LLBaseURLs(url),[AccountTool  shareAccountTool].currentAccount.authCode);
            [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
            [SVProgressHUD setBackgroundColor:ColorRGB(4, 4, 4)];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
            [SVProgressHUD showWithStatus:LLLoading];
            
            
            [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
                [SVProgressHUD dismiss];
                if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
                    [self  createAlertViewWithTitle:nil message:@"兑换成功" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
                    self.starinputTextField.text= @"";
                    [self setNetWork];
                }
            } Fail:^(NSError *Error) {
                 [SVProgressHUD dismiss];
            }];
            


        }else{
            
            [self createAlertViewWithTitle:nil message:@"你所兑换的红花不足" preferredStyle:(UIAlertControllerStyleAlert) disapperaTime:1 andDis:nil];
            
        }
        

        
        
        
        
    }
    
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];   
    
    
}


@end
